<?php
/**
 * Created by PhpStorm.
 * User: dingjinlai
 * Date: 2019/4/1
 * Time: 上午9:53
 */
/**
 * 易仓开放接口测试类 -v2
 * @author WangWei  2017-6-19 11:44:37
 */
class Eccang
{
    const _ebDomain = 'http://tuliang-eb.eccang.com';//eb域名
    const _wmsDomain = 'http://tuliang.eccang.com';//wms域名
    const _userName = 'admin';//账号名
    const _userPass  = 'QE4opraf7';//密码
    /**
     * 查询订单列表示例
     */
    public function getOrderList(){
        $service = 'getOrderList';
        $params = array(
//                 'page'=>'1',
//                 'pageSize'=>'2',
            'condition'=>array(
                'saleOrderCodes'=>array('302173175674-1364094976020')
//                         'saleOrderCodes'=>array('171646006986-1343733102007')
            )
        );
        $re = $this->SoapRequest($service,'eb',$params);
        print_r($re);
    }

    public function getSupplierInfo($params = array()) {
        $service = 'getSupplierList';

        $re = $this->SoapRequest($service,'wms',$params);

        return $re;
    }

    
    //添加供应商
    public function syncSupplierInfo($data){
        $service = 'syncSupplierInfo';
        return $this->SoapRequest($service,'wms',$data);
    }

    public function getProductBySku($params = array()) {
        $service = 'getProductBySku';

        $re = $this->SoapRequest($service,'wms',$params);

        return $re;
    }

    public function syncProduct($goods  = array())
    {
        $service = 'syncProduct';

        $data = array(
            'actionType' => 'ADD',
            'productSku' => $goods['sku_sn'],
            'productTitle' => $goods['goods_name'],
            'productTitleEn' => $goods['cn_title'],
            'pdOverseaTypeCn' => $goods['cat_name'],
            'pdOverseaTypeEn' => $goods['cat_name_zh'],
            'productDeclaredValue' => 2,
            'pdDeclareCurrencyCode' => 'USD',
            'productPurchaseValue' => $goods['in_price'],
            'productPurchaseValueNotTax' => '',
            'currencyCode' => 'RMB',
            'productWeight' => $goods['goods_weight'],
            'defaultSupplierCode' => 'G' . $goods['suppliers_id'],
            'productPrice' => $goods['shop_price'],
            'oprationType' => 2,
            'productStatus' => 1,
            'spProductAddress' => $goods['supp_url'],
            'productImgUrlList' => array(
                $goods['original_img']
            ),
            'userOrganizationId' => '',
            'defaultWarehouseId' => '',
            'eanCode' => '',
            'puc_id' => ''
        );

        $this->SoapRequest($service,'wms',$data);
    }

    //查询供应商
    public function getSupplierLista($data){
        $service = 'getSupplierList';
        return $this->SoapRequest($service,'wms',$data);
    }

    public function getProductColor(){
        $service = 'getProductColor';
        return $this->SoapRequest($service,'wms');
    }
    public function getProductSize(){
        $service = 'getProductSize';
        return $this->SoapRequest($service,'wms');
    }
    /**
     * 查询仓库示例
     */
    public function getWarehouse(){
        $service = 'getWarehouse';
        $params = array();
        $re = $this->SoapRequest($service,'eb',$params);
        print_r($re);
    }

    public function getProductInventory($params = array())
    {
        $service = 'getProductInventory';

        $re = $this->SoapRequest($service,'wms',$params);

        if ($re['code'] == 200) {
            return $re['data'];
        }

        return false;
    }

    public function getPurchaseOrders($params = array())
    {
        $service = 'getPurchaseOrders';

        $re = $this->SoapRequest($service,'wms',$params);

        if ($re['code'] == 200) {
            return $re;
        }

        return false;
    }
    //商品sku下架
    public function syncProduct_edit($productSku,$pdOverseaTypeCn,$pdOverseaTypeEn,$status){
        $service = 'syncProduct';
        $val['actionType'] = 'EDIT';
        $val['productSku'] = $productSku;
        $val['pdOverseaTypeCn'] = $pdOverseaTypeCn;
        $val['pdOverseaTypeEn'] = $pdOverseaTypeEn;
        $val['saleStatus'] = $status;
        $re = $this->SoapRequest($service,'wms',$val);
        return $re;
    }

    //商品修改图片
    public function syncProduct_edit_img($productSku,$productImgUrlList,$pdOverseaTypeCn,$pdOverseaTypeEn,$saleStatus=2){
        $service = 'syncProduct';
        $val['actionType'] = 'EDIT';
        $val['productSku'] = $productSku;
        $val['productImgUrlList'] = $productImgUrlList;
        $val['pdOverseaTypeCn'] = $pdOverseaTypeCn;
        $val['pdOverseaTypeEn'] = $pdOverseaTypeEn;
        $re = $this->SoapRequest($service,'wms',$val);
        return $re;
    }

    /**
     * 异步修改销售状态
     * @param $productSku
     * @param $saleStatus
     * @param $pdOverseaTypeCn
     * @param $pdOverseaTypeEn
     *
     * @return array
     * syncEditSaleStatus
     * author: walker
     * Date: 2019/12/4
     * Time: 10:32
     * Note:
     */
    public function syncEditSaleStatus($productSku,$saleStatus,$pdOverseaTypeCn,$pdOverseaTypeEn,$spProductAddress,$defaultSupplierCode,$productPurchaseValue)
    {
        $service = 'syncProduct';
        $val['actionType'] = 'EDIT';
        $val['productSku'] = $productSku;
        $val['saleStatus'] = $saleStatus;
        $val['pdOverseaTypeCn'] = $pdOverseaTypeCn;
        $val['pdOverseaTypeEn'] = $pdOverseaTypeEn;
        $val['spProductAddress'] = $spProductAddress;
        $val['defaultSupplierCode'] = $defaultSupplierCode;
        $val['productPurchaseValue'] = $productPurchaseValue;
        $re = $this->SoapRequest($service,'wms',$val);
        return $re;
    }

    public function baby_syncEditSaleStatus($val){
        $service = 'syncProduct';
        $val['actionType'] = 'EDIT';
        // $val['productSku'] = $productSku;
        // $val['saleStatus'] = $saleStatus;
        // $val['pdOverseaTypeCn'] = $pdOverseaTypeCn;
        // $val['pdOverseaTypeEn'] = $pdOverseaTypeEn;
        // $val['spProductAddress'] = $spProductAddress;
        // $val['defaultSupplierCode'] = $defaultSupplierCode;
        // $val['productPurchaseValue'] = $productPurchaseValue;
        $re = $this->SoapRequest($service,'wms',$val);
        return $re;
    }

    /** 同步商品规格图片
     * @param $productSku
     * @param $productImgUrlList
     * @param $pdOverseaTypeCn
     * @param $pdOverseaTypeEn
     * @return array
     */
    public function syncProduct_attr_img($productSku,$productImgUrlList,$pdOverseaTypeCn,$pdOverseaTypeEn,$productTitle,$personOpraterId){
        $service = 'syncProduct';
        $val['actionType'] = 'EDIT';
        $val['productSku'] = $productSku;
        $val['productTitle'] = $productTitle;
        $val['productImgUrlList'] = $productImgUrlList;
        $val['pdOverseaTypeCn'] = $pdOverseaTypeCn;
        $val['pdOverseaTypeEn'] = $pdOverseaTypeEn;
        $val['personOpraterId'] = $personOpraterId;
        // print_R($val);die;
        $re = $this->SoapRequest($service,'wms',$val);
        return $re;
    }



    public function add_syncProduct($data){
        $service = 'syncProduct';

        // //获取货品的规格图片
        // $CI=&get_instance();
        // $CI->load->library('good');
        // /** @var good $good_lib */
        // $good_lib = $CI->good;
        // $img_url = $good_lib->get_sku_attr_img($data['productSku']);

        // if($img_url){
        //     $data['productImgUrlList'][] = $img_url;
        //     // array_unshift($data['productImgUrlList'], $img_url);
        // }

        return $this->SoapRequest($service,'wms',$data);
    }

    public function aa($page){
        $service = 'getProductList';

        // $val['saleStatus'] = 1;
        // $val['defaultSupplierCode'] = '';
        $val['page'] = $page;
        $re = $this->SoapRequest($service,'wms',$val);
        return $re;
    }

    public function same_list_sku($data){
        $service = 'getProductList';
        $re = $this->SoapRequest($service,'wms',$data);
        return $re;
    }


    public function getSaleStatus(){
        $service = 'getSaleStatus';
        return $this->SoapRequest($service,'wms');
    }

    public function getProductInventorys($data){
        $service = 'getProductInventory';
        // $data['pageSize'] = 1000;
        // $data['page'] = 8;
        return $this->SoapRequest($service,'wms',$data);
    }
    
    //E仓订单获取
    public function order_handle($data){
        $service = 'getOrderList';
        $res = $this->SoapRequest($service,'EB',$data);
        return $res;
    }
    //E仓退款订单
    public function order_handle_refund($data){
        $service = 'rmaRefundList';
        $res = $this->SoapRequest($service,'EB',$data);
        return $res;
    } 

    //E仓映射
    public function modifySkuRelation($data){
        $service = 'modifySkuRelation';
        $res = $this->SoapRequest($service,'EB',$data);
        return $res;
    } 

    /**
     * Soap请求
     * @param string $service 方法名
     * @param string $systemCode 所属系统代码
     * @param array $params 请求参数
     * @return array
     */
    public function SoapRequest($service,$systemCode,$params=array()){
        ini_set('default_socket_timeout', 3000);

        // j_write_log($service, 'ecang');
        // j_write_log($systemCode, 'ecang');
        // j_write_log($params, 'ecang');
        
        $return = array(
            'ask'=>'Failure',
            'message'=>'SoapRequest Error',
            'data'=>array()
        );
        $systemCode = strtoupper($systemCode);//转大写
        if(!in_array($systemCode,array('EB','WMS'))){
            $return['message'] = 'systemCode Error';
            return $return;
        }
        $domain = $systemCode=='EB' ? self::_ebDomain : self::_wmsDomain;
        if(empty($domain)){
            $return['message'] = 'domain Empty';
            return $return;
        }
        $domain = trim($domain,'/');//去除斜线
        $wsdl = $domain.'/default/svc-open/web-service-v2';
        $method = 'callService';
        $req = array(
            'userName'=>self::_userName,
            'userPass'=>self::_userPass,
            'service'=>$service
        );
        if(!empty($params)){
            $req['paramsJson'] = json_encode($params);
        }
        try {
            $option=array(
                "trace" => true, //是否启用调试信息，true或false     __getLastResponse()。
            );
            $client = new SoapClient($wsdl,$option);
            $re = $client->__soapCall($method,array('parameters'=>$req));
//            $requestInfo = $client->__getLastRequest();
//            $responseInfo = $client->__getLastResponse();
//             var_dump($requestInfo);
//             var_dump($responseInfo);
            $response = $re->response;
            $return = json_decode($response,1);
        } catch (SoapFault $e) {
            $return['message'] =  $e->getMessage();
        }

        // j_write_log($response, 'ecang');
        // j_write_log($return, 'ecang');
        return $return;
    }
}
